<?php

namespace App\Http\Controllers;

use App\Models\DomainList;
use App\Models\FlowChannel;
use App\Models\FlowChannelStat;
use App\Models\Setting as SettingModel;
use Carbon\Carbon;
use Encore\Admin\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;

class WebController extends Controller
{
    public function detail(Content $content, Request $request, Row $row)
    {


        $param = $request->param;
        $flow_channel = FlowChannel::query()->where('param', $param)->first();
        if (is_null($flow_channel)) {
            return '';
        }
        $start_date = Carbon::now()->subDays(7)->toDateString();
        $end_date = Carbon::now()->toDateString();

        $rows = FlowChannelStat::query()->groupBy('date')->whereBetween('date', [$start_date, $end_date])
            ->select('date')
            ->where('flow_channel_id', $flow_channel->id)
            ->selectRaw('sum(pv) as total_pv')
            ->selectRaw('sum(ip_num) as total_ip_num')
            ->selectRaw('sum(uv_num) as total_uv_num')
            ->selectRaw('sum(pv)/sum(ip_num) as pu_rate')
            ->selectRaw('sum(divide_amount) as total_amount')
            ->selectRaw('sum(read_num_incr)/sum(pv) as daoda')
            ->orderByDesc('date')
            ->get()->each(function ($row) {
                $row->qianpv = $row->total_pv == 0 ? 0 : round(($row->total_amount / $row->total_pv) * 1000, 4);
                $row->qianip = $row->total_ip_num == 0 ? 0 : round(($row->total_amount / $row->total_ip_num) * 1000, 4);
            })->toArray();
        $headers = ['日期', 'PV', 'IP', 'UV', 'PV/IP', '结算金额', '到达率', '千PV产值', '千IP产值'];
        $table = new Table($headers, $rows);
        $tab = new Tab();
        $tab->add($flow_channel->name . '-收益数据', $table);

        $daoda_headers = ['日期', '小时', '到达率', 'PV', 'IP', 'UV', 'PU比'];
        $daoda_rows = FlowChannelStat::query()->groupBy('date', 'hour')->orderByDesc('date')
            ->select(['date', 'hour'])
            ->where('flow_channel_id', $flow_channel->id)
            ->where('date', Carbon::now()->toDateString())
            ->selectRaw('sum(read_num_incr)/sum(pv) as daoda')
            ->selectRaw('sum(pv) as total_pv')
            ->selectRaw('sum(ip_num) as total_ip_num')
            ->selectRaw('sum(uv_num) as total_uv_num')
            ->selectRaw('sum(pv)/sum(ip_num) as pu_rate')
            ->get()->toArray();
        $daoda_table = new Table($daoda_headers, $daoda_rows);
        $daoda_tab = new Tab();
        $daoda_tab->add($flow_channel->name . '-到达率明细', $daoda_table);

        return '<title>' . $flow_channel->name . '</title>' . Admin::css() . $tab->render() . $daoda_tab->render();
    }

    public function detailApi(Content $content, Request $request, Row $row)
    {


        $param = $request->param;
        $flow_channel = FlowChannel::query()->where('param', $param)->first();
        if (is_null($flow_channel)) {
            return '';
        }
        $start_date = Carbon::now()->subDays(7)->toDateString();
        $end_date = Carbon::now()->toDateString();

        $rows = FlowChannelStat::query()->groupBy('date')->whereBetween('date', [$start_date, $end_date])
            ->select('date')
            ->where('flow_channel_id', $flow_channel->id)
            ->selectRaw('sum(pv) as total_pv')
            ->selectRaw('sum(ip_num) as total_ip_num')
            ->selectRaw('sum(uv_num) as total_uv_num')
            ->selectRaw('sum(pv)/sum(ip_num) as pu_rate')
            ->selectRaw('sum(divide_amount) as total_amount')
            ->selectRaw('sum(read_num_incr)/sum(pv) as daoda')
            ->orderByDesc('date')
            ->get()->each(function ($row) {
                $row->valid_pv = $row->total_pv == 0 ? 0 : intval($row->total_pv * $row->daoda);
                $row->qianpv = $row->total_pv == 0 ? 0 : round(($row->total_amount / $row->valid_pv) * 1000, 4);
                $row->qianip = $row->total_ip_num == 0 ? 0 : round(($row->total_amount / $row->total_ip_num) * 1000, 4);

            })->toArray();
        return $rows;


    }

    public function getUrl(Request $request)
    {
        $flow_channel = FlowChannel::query()->where('param', $request->param)->first();

        $pre_use = json_decode(SettingModel::query()->firstOrCreate(['key' => 'pre_use'], ['value' => null])->value);

        $domain = DomainList::query()->orderBy('id')->where('state', 0)->limit($pre_use)->get()->random();

        return ['url' => 'http://' . $domain->domain . '/?fc_id=' . $flow_channel->id];


    }


}
